from random import randint

from django.http import HttpResponse
from django_redis import get_redis_connection
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from celery_tasks.sms.tasks import send_sms_code
from libs.captcha.captcha import captcha
from libs.yuntongxun.sms import CCP
from .serializers import RegisterSMSCodeSerializer


class RegisterImageCodeView(APIView):
    def get(self,request,image_code_id):
        """
        通过第三方库,生成图片和验证码,我们需要对验证码进行redis保存
        思路为:
        创建图片和验证码
        过redis进行保存验证码,需要在设置中添加 验证码数据库选项
        将图片返回
        """
        # 创建图片和验证码
        text,image = captcha.generate_captcha()
        # 通过redis进行保存验证码
        redis_conn = get_redis_connection('code')
        redis_conn.setex('img_'+image_code_id,60,text)
        # 将图片返回
        # 注意,图片是二进制,我们通过HttpResponse返回
        return HttpResponse(image,content_type='image/jpeg')

# 短信验证码
class RegisterSMSCodeView(GenericAPIView):
    """
    创建序列化器,定义text 和 image_code_id
    redis 判断该用户是否频繁获取
    生成短信验证码
    redis增加记录
    发送短信
    返回响应
    """
    serializer_class = RegisterSMSCodeSerializer

    def get(self,request,mobile):
        serializer = self.get_serializer(data = request.query_params)
        serializer.is_valid(raise_exception = True)
        #链接redis
        redis_conn = get_redis_connection('code')
        #是否频繁获取验证码
        if redis_conn.get('sms_flag_%s'%mobile):
            return Response(status=status.HTTP_429_TOO_MANY_REQUESTS)
        #获取验证码
        sms_code = '%06d'%randint(0,999999)

        redis_conn.setex('sms_%s'%mobile,5*60,sms_code)
        redis_conn.setex('sms_flag_%s'%mobile,60,1)
        #发送短信
        ccp = CCP()
        ccp.send_template_sms(mobile,[sms_code,5],1)
        send_sms_code.delay(mobile,sms_code)
        #返回响应
        return Response({'message':'ok'})
